package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.dao.AdminDao;
import com.pojo.AdminPojo;
import com.util.Log;


public class AdminLogin extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession(true);
		
		String uname = request.getParameter("uname");
		String pwd = request.getParameter("pwd");
		String code = (String)session.getAttribute("currcode");
		String number = request.getParameter("number");
		String message;
		if (uname.equals("")){
			message = "请输入登录名！";
			session.invalidate();
		} else if (pwd.equals("")){
			message = "请输入密码！";
			session.invalidate();
		} else if (!number.equalsIgnoreCase(code)){
			message = "您输入的验证码不正确！";
			session.invalidate();
		} else {
			AdminDao dao = new AdminDao();
			AdminPojo admin = dao.checkPwd1(uname, pwd) ;
			dao.closeAll();
            Log.write(" 管理员:"+uname+" 进行登录请求");			
			if(admin.getName() == "" || admin.getName() == null){
				message = "用户名、密码不正确！";
				session.invalidate();
			}else{
				session.setAttribute("curradmin", uname);
				message = "登录成功";
			}
		}
		String json = "{\"curradmin\":\""+uname+"\",\"status\":\""+message+"\"}";
		Log.write(json);
		out.println(json);
		out.flush();
        out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
